package com.app.couponsharelife.ui.adapter

import android.content.Context
import android.content.Intent
import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.app.couponsharelife.R
import com.app.couponsharelife.data.bean.LifeShopRecord
import com.app.couponsharelife.manage.Contacts
import com.app.couponsharelife.ui.main.nativelife.activity.NativeLifeDetailsActivity
import kotlinx.android.synthetic.main.lifesquare_baservitm.view.*

/**
 *Created by xct on 2019/1/10
 *Description:
 */
class LifeShopListAdapter(private var context: Context,private var dats:List<LifeShopRecord>):RecyclerView.Adapter<LifeShopListAdapter.MyHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
        val view= LayoutInflater.from(context).inflate(R.layout.lifesquare_baservitm,parent,false)
        return MyHolder(view)
    }

    override fun getItemCount(): Int {
        return dats.size
    }

    override fun onBindViewHolder(holder: MyHolder, position: Int) {
        if (dats.size>0){
            val lifeCardBean = dats[position]
            holder.tvName.text=lifeCardBean.shopName?:""
            holder.tvClassName.text=lifeCardBean.className?:""
            holder.tvJL.text=""
            loadImag(holder.imageLogo, lifeCardBean.logo?:"", R.drawable.shop_logo_error)
            val imagUrl = if (lifeCardBean.images!=null&&lifeCardBean.images!!.isNotEmpty()){
                val imags=lifeCardBean.images!!.split(",")
                imags[0]
            }else{
                lifeCardBean.images!!
            }
            loadImag(holder.imageView, imagUrl, R.drawable.lifereferrals_bannenr)
            if (lifeCardBean.activity!=null){
                holder.tvTitle.text= Html.fromHtml("<font color=\"#9000FC\">#热门活动#</font>" + lifeCardBean.activity!!.introduct)
            }
            if (lifeCardBean.maxReduceMoney!=null&&lifeCardBean.maxReduceMoney!!>0){
                holder.tvPrice.text="￥"+lifeCardBean.maxReduceMoney
                holder.tvPrice.visibility=View.VISIBLE
            }else{
                holder.tvPrice.visibility=View.GONE
            }
            if (lifeCardBean.minDiscount!=null&&lifeCardBean.minDiscount!!>0){
                holder.tvZK.text=lifeCardBean.minDiscount.toString()+"折"
                holder.tvZK.visibility=View.VISIBLE
            }else{
                holder.tvZK.visibility=View.GONE
            }
            if (lifeCardBean.giftNum!=null&&lifeCardBean.giftNum!!>0){
                holder.tvFlName.visibility=View.VISIBLE
            }else{
                holder.tvFlName.visibility=View.GONE
            }
            holder.itemView.setOnClickListener {
                val intent= Intent(context, NativeLifeDetailsActivity::class.java)
                intent.putExtra("ShopID",lifeCardBean.id.toString())
                context.startActivity(intent)
            }
        }
    }
    private fun loadImag(imageView: ImageView, url:String, errorimag:Int){
        var imgUrl=if (url.startsWith("//")){
            Contacts.TCP +url
        }else if(url.startsWith("http")){
            url
        }else{
            Contacts.REMOTE_IMAGE_URL +url
        }
        Glide.with(context)
                .asBitmap()
                .apply(RequestOptions().error(errorimag))
                .load(imgUrl)
                .into(imageView)
    }
    inner class MyHolder(itemView:View):RecyclerView.ViewHolder(itemView){
        var imageView=itemView.lifereferrals_rvitem_imag
        var imageLogo=itemView.lifereferrals_rvitem_logo
        var tvName=itemView.lifereferrals_rvitem_shopname
        var tvTitle=itemView.lifereferrals_rvitem_title
        var tvClassName=itemView.lifereferrals_rvitem_classname
        var tvJL=itemView.lifereferrals_rvitem_jl
        var tvZK=itemView.lifereferrals_rvitem_zk
        var tvPrice=itemView.lifereferrals_rvitem_price
        var tvFlName=itemView.lifereferrals_rvitem_flname
    }
}